/**
 * 日志工具模块
 */

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf, colorize } = format;

// 自定义日志格式
const myFormat = printf(({ level, message, timestamp }) => {
  return `[${timestamp}] ${level}: ${message}`;
});

// 创建logger实例
const logger = createLogger({
  level: 'info',
  format: combine(
    timestamp(),
    myFormat
  ),
  defaultMeta: { service: 'mcp-tool' },
  transports: [
    // 输出到控制台
    new transports.Console({
      format: combine(
        colorize(),
        timestamp(),
        myFormat
      )
    }),
    // 输出到文件
    new transports.File({
      filename: 'error.log',
      level: 'error',
      dirname: process.cwd()
    }),
    new transports.File({
      filename: 'combined.log',
      dirname: process.cwd()
    })
  ]
});

// 设置日志级别
exports.setLevel = (level) => {
  logger.level = level;
};

// 导出logger实例
module.exports = logger;